//
//  LoadingViewController.m
//  WHB_logist
//
//  Created by 徐国庆 on 16/1/6.
//  Copyright (c) 2016年 turing. All rights reserved.
//

#import "LoadingViewController.h"
#import "ForgetPassWordCell.h"
#import "ForgetPassWordViewController.h"
#import "User.h"
#import "RegisterViewController.h"
@interface LoadingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray     *imagesNameArray;
@property(nonatomic, strong) NSArray     *labelContentArray;

@end

@implementation LoadingViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imagesNameArray = [NSArray arrayWithObjects:@"mi-1", @"mi-1", nil];
        self.labelContentArray = [NSArray arrayWithObjects:@"请输入手机号", @"请输入密码", nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initWithView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifer = @"forgetPassWordCell";
    ForgetPassWordCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        cell = [[ForgetPassWordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
    }
    //    cell.textLabel.text = @"fasdf";
    cell.textImageView.image = [UIImage imageNamed:[self.imagesNameArray objectAtIndex:indexPath.row]];
    cell.contentTextField.placeholder = [self.labelContentArray objectAtIndex:indexPath.row];
    cell.lineLabel.backgroundColor = [UIColor HexStrWithColor:@"999999"];
    cell.QrCodeButton.layer.masksToBounds = YES;
    cell.QrCodeButton.layer.cornerRadius = (cell.frame.size.height - 20) / 2;
    cell.QrCodeButton.layer.borderColor = [UIColor HexStrWithColor:@"f8cc00"].CGColor;
    cell.QrCodeButton.layer.borderWidth = 1.5;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 1) {
        cell.QrCodeButton.hidden = YES;
    } else if (indexPath.row == 0){
        cell.QrCodeButton.hidden = YES;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

#pragma mark 头
- (UIView *)headerView{
    
    UIView *bottom = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    bottom.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kUIScreenWidth - 100) / 2, (bottom.frame.size.height - 100) / 2, 100, 100)];
    imageView.image = [UIImage imageNamed:@"logo"];
    [bottom addSubview:imageView];
    return bottom;
}

#pragma mark 获取验证码点击事件
- (void)qrCodeButtonAction:(UIButton *)button{
    NSLog(@"获取验证码");
    [button setTitle:@"59秒" forState:UIControlStateNormal];
}

#pragma mark 返回按钮点击事件
- (void)returnButtonAction:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


/**
 *  初始化视图
 */
- (void)initWithView{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 导航栏
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kStateHeight + kNavHeight)];
    topView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:topView];
    
    // 导航栏标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, kUIScreenWidth, 40)];
    titleLabel.textColor = [UIColor HexStrWithColor:@"999999"];
    titleLabel.text = @"忘记密码";
    titleLabel.textAlignment = 1;
    titleLabel.font = [UIFont systemFontOfSize:15 weight:2];
    [topView addSubview:titleLabel];
    
    // 导航栏左侧返回按钮
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.frame = CGRectMake(0, 25, 30, 30);
    [returnButton setImage:[UIImage imageNamed:@"zuo"] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:returnButton];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kStateHeight + kNavHeight + 5, kUIScreenWidth, kUIScreenHeight - kNavHeight - 90 - kStateHeight + 40) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [self footerView];
    self.tableView.tableHeaderView = [self headerView];
    //    self.tableView.backgroundColor = [UIColor HexStrWithColor:@"999999"];
    self.tableView.backgroundColor = [UIColor whiteColor];
}


// 脚视图
- (UIView *)footerView{
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, kUIScreenWidth, 150)];
    
    // 登陆
    UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    footerButton.frame = CGRectMake(20, 40, kUIScreenWidth - 40, 45);
    footerButton.layer.masksToBounds = YES;
    footerButton.layer.cornerRadius = 5;
    [footerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerButton setTitle:@"登陆" forState:UIControlStateNormal];
    footerButton.backgroundColor = [UIColor HexStrWithColor:@"653239"];
    [footerButton addTarget:self action:@selector(footerButtonAction:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:footerButton];
    
    // 新用户注册
    UIButton *newUserButton = [UIButton buttonWithType:UIButtonTypeCustom];
    newUserButton.frame = CGRectMake(20, 100, 80, 30);
    newUserButton.layer.masksToBounds = NO;
    newUserButton.layer.cornerRadius = 5;
    [newUserButton setTitleColor:[UIColor HexStrWithColor:@"653239"] forState:UIControlStateNormal];
    [newUserButton setTitle:@"新用户注册" forState:UIControlStateNormal];
    newUserButton.backgroundColor = [UIColor whiteColor];
    [newUserButton addTarget:self action:@selector(newUserRegisterButtonAction:) forControlEvents:UIControlEventTouchDown];
    newUserButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:newUserButton];
    
    // 忘记密码
    UIButton *forgetPassWord = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetPassWord.frame = CGRectMake(bottomView.frame.size.width - 90, 100, 80, 30);
    forgetPassWord.layer.masksToBounds = NO;
    forgetPassWord.layer.cornerRadius = 5;
    [forgetPassWord setTitleColor:[UIColor HexStrWithColor:@"653239"] forState:UIControlStateNormal];
    [forgetPassWord setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [forgetPassWord addTarget:self action:@selector(forgetPassWordButtonAction:) forControlEvents:UIControlEventTouchDown];
    forgetPassWord.titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:forgetPassWord];
    
    return bottomView;
}

#pragma mark 登陆按钮点击事件
- (void)footerButtonAction:(UIButton *)button{
    
    [self userLogin];
}

#pragma mark 新用户注册点击事件
- (void)newUserRegisterButtonAction:(UIButton *)button{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma mark 忘记密码点击事件
- (void)forgetPassWordButtonAction:(UIButton *)button{
    ForgetPassWordViewController *forgetPassWordVC = [[ForgetPassWordViewController alloc] init];
    [self.navigationController pushViewController:forgetPassWordVC animated:YES];
}

/**
 *  用户登陆
 */
- (void)userLogin{
    NSString *tempStr = [UtilsHelper MD5:@"123456"];
    [DataController userLogin:@"13704809999"
                     password:tempStr
                    loginType:@"2"
                         imei:kIdentifierString
                    withBlock:^(NSDictionary *result) {
                        NSLog(@"result == %@ == %@", result, [result objectForKey:@"resMsg"]);
                        if ([[result objectForKey:@"resCode"] isEqualToString:@"0"]) {
                            NSDictionary *data = [result objectForKey:@"data"];
                            User *user = [MTLJSONAdapter modelOfClass:[User class] fromJSONDictionary:data error:nil];
                            
                            //持久化用户登录信息
                            [NSKeyedArchiver archiveRootObject:user toFile:kUserPath];
                            [self dismissViewControllerAnimated:YES completion:^{
                                
                            }];
                            //                            [[NSUserDefaults standardUserDefaults] setObject:self.phoneNumberTextField.text forKey:@"accountNumber"];
                            //                            [[NSUserDefaults standardUserDefaults] setObject:@"123456" forKey:@"passWord"];
                            //                            [self dismissViewControllerAnimated:YES completion:^{
                            //
                            //                            }];
                            [self.navigationController popViewControllerAnimated:YES];
                        } else if ([[result objectForKey:@"resCode"] isEqualToString:@"2"]){
                            NSLog(@"踢掉前一个用户");
                            [self userLogin];
                        }else if ([[result objectForKey:@"resCode"] isEqualToString:@"3"]){
                            NSLog(@"密码输入错误三次!");
                        }
                    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
